
*** Set Fixed Effects and Clustering
global fe "courtq"
global clusvar "partyr"


*** Set general controls
global exactcontrols "black hisp female blackfemale hispfemale  ageatfiling age2 age3 sh_hhpov med_grossrent rentowed multicase"
global healthvars "numhosp pre_anymental pre_nonmental"
global controls "$exactcontrols $healthvars" 

capture frame drop numhealth 
frame create numhealth str55 outcome str32 specification  beta se N 

global numoutlist "num_anymental1y num_anymental2y num_anymental3t4y  numnonpreg1y numnonpreg2y numnonpreg3t4y numED1yr numED2yr numED3t4yr"

foreach spec in ols rf iv meansd {
	foreach out in $numoutlist {

	if "`spec'"=="ols" {
		reghdfe `out' evictionorder $controls if samp==1, absorb($fe) cluster($clusvar)
		frame post numhealth ("`out'") ("`spec'") (_b[evictionorder]) (_se[evictionorder]) (e(N))
	}
	
	if "`spec'"=="rf" {
		reghdfe `out' stringency $controls if samp==1 [pw=cw], absorb($fe) cluster($clusvar)
		frame post numhealth ("`out'") ("`spec'") (_b[stringency]) (_se[stringency]) (e(N))
	}

	if "`spec'"=="iv" {
		ivreghdfe `out' $controls (evictionorder =stringency) if samp==1, absorb($fe) cluster($clusvar)
		frame post numhealth ("`out'") ("`spec'") (_b[evictionorder]) (_se[evictionorder]) (e(N))
	}
	
	if "`spec'"=="meansd" {
		sum `out' if e(sample)==1 & evictionorder==0
		frame post numhealth ("`out'") ("`spec'") (`=r(mean)') (`=r(sd)') (r(N))
	}
}
}

frame change numhealth 

gen label=""

replace label="Num Mental Health" if regexm(outcome, "anymental")==1
replace label="Num Hospital Visits" if regexm(outcome, "numnonpreg")==1
replace label="Num Emergency Visits" if regexm(outcome, "numED")==1

gen time="Q01Q04" if regexm(outcome, "1y")==1
replace time="Q09Q16" if regexm(outcome, "3t4y")==1
replace time="Q05Q08" if regexm(outcome, "2y")==1 & time==""

order label time specification bet se N 
drop outcome

rename label outcome

save ${Inputs}/NY_hospital_ivols, replace


